package com.thinkgsp.webapp.controller.base;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.jasperreports.engine.JRDataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.thinkgsp.jxc.model.Dept;
import com.thinkgsp.jxc.report.JxcBeanFactory;
import com.thinkgsp.jxc.service.DeptService;
import com.thinkgsp.webapp.controller.BaseController;
import com.thinkgsp.webapp.util.ExtReturn;

@Controller
@RequestMapping(value = "/base")
public class DeptController extends BaseController {
	
	@Autowired
	private DeptService deptService;
	

	@RequestMapping("/deptlist.do")
    @ResponseBody
    public List<Dept> getEmps(HttpServletRequest request,
			HttpServletResponse response){
		List<Dept> depts = deptService.getAllDept();
		return depts;
	}
	
	@RequestMapping(value = "/adddept.do")
	@ResponseBody
	public Object add(Model model ,@RequestBody Dept dept) {
		try {
			this.deptService.saveDept(dept); 
			loger.debug("update dept");
			return new ExtReturn(true,"部门修改成功");
		} catch (Exception e) {
			return new ExtReturn(false,"部门修改出错",e.getMessage());
		}
	}
	
	@RequestMapping(value = "/upddept.do",method = RequestMethod.POST )
	@ResponseBody
	public Object upd(Model model ,@RequestBody Dept dept) {
		try {
			this.deptService.saveDept(dept); 
			this.loger.debug("update dept");
			return new ExtReturn(true,"部门修改成功");
		} catch (Exception e) {
			return new ExtReturn(false,"部门修改出错",e.getMessage());
		}	
	}
	@RequestMapping(value = "/deldept.do")
	@ResponseBody
	public Object del(HttpServletRequest request,
			HttpServletResponse response) {
		try {
			Integer deptId=Integer.decode(request.getParameter("deptId"));
			this.deptService.deleteDept(deptId);
			return new ExtReturn(true,"删除部门成功");
		} catch (Exception e) {
			return new ExtReturn(false,"删除部门出错",e.getMessage());
		}
	} 
}
